<template>
  <div class="form-container bg-fff">
    <el-form :model="form" ref="form" label-width="120px" label-position="right" :rules="rules">
      <el-form-item label="手机号码" prop="phone">
        <el-input class="line-input" maxlength="11" v-model="form.phone" placeholder="请输入手机号码"></el-input>
      </el-form-item>
      
      <el-form-item label="手机验证码" prop="verificationCode">
        <el-input class="line-input" maxlength="6" v-model="form.verificationCode" placeholder="请输入手机验证码">
          <v-message-code slot="suffix" @click.native="getMessageCode" ref="messageCode" class="edit-el-intput-suffix"
                          :phoneNum="form.phone"></v-message-code>
        </el-input>
      </el-form-item>
      
      <el-form-item label="新密码" prop="password">
        <el-input class="line-input" type="password" v-model="form.password" placeholder="请输入新密码"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="confirmPassword">
        <el-input class="line-input" type="password" v-model="form.confirmPassword" placeholder="请再次输入新密码"></el-input>
      </el-form-item>
    </el-form>
    <div class="operation">
      <el-button type="primary" plain @click="valid">提交</el-button>
    </div>
  </div>
</template>
<script lang="ts">
  import { Vue, Component } from 'vue-property-decorator'
  import vMessageCode from '@/components/message-code'
  import md5 from 'md5'

  @Component({
    components: {
      vMessageCode
    }
  })
  export default class Process0 extends Vue {
    form = {
      phone: '',
      verificationCode: '',
      password: '',
      confirmPassword: '',
    }
    rules = {
        phone: [
            { required: true, message: '请输入手机号码', trigger: 'blur' },
            {
            pattern: /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/,
            message: '手机号码错误',
            trigger: 'blur'
            }
        ],
        verificationCode: [
            { required: true, message: '请输入手机验证码', trigger: 'blur' }
        ],
        password: [
            { required: true, message: '请输入新密码', trigger: 'blur' }
        ],
        confirmPassword: [
            { required: true, message: '请再次输入新密码', trigger: 'blur' },
            { validator: this.testConfirmPassword, trigger: 'blur' }
        ]
    }

    testConfirmPassword (rule, value, cb) {
      if (this.form.password === '') {
        cb(new Error("请先输入密码"))
      } else {
        if (value === this.form.password) {
          cb()
        } else {
          cb(new Error("您两次输入的密码不一致,请重新输入"))
        }
      }
    }

    getMessageCode () {
      this.$refs.form.validateField('phone', phoneValid => {
        if (!phoneValid) {
          this.$refs.messageCode.start()
        }
      })
    }

    async post (form) {
      form = {
        ...form,
        password: md5(form.password).toLocaleUpperCase(),
        confirmPassword: md5(form.password).toLocaleUpperCase()
      }
      let res: AjaxResponse = await this.$axios.post('g/gateway/channelprovider/resetpassword', form)
      this.$utils.tips(res)
      if (res.responseCode === 200) {
        this.$router.push('/login')
      }
    }

    valid () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.post(this.form)
        }
      })
    }

    
  }

</script>

<style lang="less" scoped>
  .form-container {
    width: 1180px;
    // margin: 0 auto;
    /* padding: 60px 0 30px; */
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
  }

  .operation {
    text-align: center;
    font-size: 14px;
    color: #999;
    em {
      color: #409EFF;
      cursor: pointer;
    }
    .el-button {
      margin-top: 30px;
    }
  }

  .el-steps {
    margin-bottom: 60px;
  }
</style>
